import InstallCommon
import os


def InstallDefaultConfigFiles():
    print "Install default COOPGIS Config File to:  " + InstallCommon.COOPGISDirectory + "? and django.wsgi and permissions changes  Y/N"
    Response = raw_input().lower()
    if(not ('y' in Response)):
        return 
    os.system("mkdir " + InstallCommon.COOPGISConfigDirectory)
    FileName = InstallCommon.COOPGISConfigDirectory +  'coopgisconfig.py'
    File=open(FileName, 'w')
    File.write(coopgisconfig_py)
    File.close()

    FileName = InstallCommon.PythonPath +  'django.wsgi'
    File=open(FileName, 'w')
    File.write(django_wsgi)
    File.close()

    os.system("chmod -R 777 /opt/coopgis")
    os.system("chmod -R 777 /var/www")
    os.system("mkdir /opt/coopgisupdate")
    os.system("chmod 777 /opt/coopgisupdate")
    os.system("mkdir /opt/coopgisnotupdated")






django_wsgi = """import os
import sys

Paths=['""" + InstallCommon.COOPGISDirectory + """', '""" + InstallCommon.PythonPath + """', '""" + InstallCommon.PythonPath + """views/']
for i in Paths:
    if not i in sys.path:
        sys.path.append(i)

os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoproject.settings'
os.system('export DJANGO_SETTINGS_MODULE=djangoproject.settings')
import django.core.handlers.wsgi
application=django.core.handlers.wsgi.WSGIHandler()"""

coopgisconfig_py = """#    Copyright 2014 Gregory Roby
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.


#************************BEGIN Add Include Files to Path************************
#SECTION MOVED to /opt/coopgis/djangoproject/apache/django.wsgi
import sys, os
ConfigurationDirectory = '/opt/coopconfig'
COOPGISDirectory = '/opt/coopgis'
DjangoProjectDirectory = COOPGISDirectory + '/djangoproject'

NeededPaths=[ConfigurationDirectory, COOPGISDirectory, DjangoProjectDirectory]
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
os.system('export DJANGO_SETTINGS_MODULE=djangoproject.settings')
#************************END   Add Include Files to Path************************
import os
COOPGISServerIndicator=os.path.exists('/opt/coopgisnotupdated/ThisIsTheServer.txt')

COOPGISPath='/opt/coopgis/'
COOPGISUpdatePath='/opt/coopgisupdate/'
COOPGISUpdatePathFTPRootReadOnly='/opt/coopgisupdate/ftprootreadonly/'
COOPGISUpdatePathFTP='/opt/coopgisupdate/ftprootreadonly/ftp/'
COOPGISUpdatePathInProgress='/opt/coopgisupdate/inprogress/'
COOPGISUpdatePathCompleted='/opt/coopgisupdate/completed/'
COOPGISUpdatePathCancelled='/opt/coopgisupdate/cancelled/'
COOPGISUpdatePathRepository='/var/www/downloadrepository/'
COOPGISUpdatePathRepositoryDownload=COOPGISUpdatePathRepository + 'coopgis/'
COOPGISUpdateLogFileName=COOPGISUpdatePath + 'update.log'
COOPGISAssemblyEditorLogFileName=COOPGISUpdatePath + 'assemblyeditor.log'
COOPGISUsersWithDelayAfterUpdating=['SCRUBBED']  #Added delay to prevent specific users from accessing COOPGIS while updates are still in progress
COOPGISUpdateWWWPath='/var/www/coopgisupdate/'
COOPGISServerIPAddress='10.0.2.15'  
COOPGISVersion=['COOPGIS', '06/7/2014', '0', '9', '0', 'Development_Python_2_7']  #Program Name, Date, Release, Major, Minor, Status
COOPGISInVirtualMachineIndicator=os.path.exists('/opt/InVirtualMachine')  #Create this folder on the Virtual Machine Image
COOPGISCommandTemporaryUploadFileName=COOPGISPath + 'command/commandupload.txt'
COOPGISCommandTemporaryUploadResultsFileName=COOPGISPath + 'command/commanduploadresult.txt'
COOPGISCommandTemporaryServerVerifiedResultsFileName=COOPGISPath + 'command/serververifiedallcommands.txt'
COOPGISTemporaryFileName=COOPGISPath + 'TemporaryFile'
COOPGISCommandUpdateTemporaryStatusFile=COOPGISPath + 'command/updatestatus.txt'
COOPGISHCIToolScanResultFile=COOPGISPath + 'command/hcitoolscanresult.txt'
COOPGISBlueToothMacAddressTargetFileName=COOPGISPath + 'scripts/ConnectToBluetooth'
COOPGISBlueToothMacAddressFileName='/opt/coopgisnotupdated/bluetoothmacaddress.txt'
COOPGISMapFilePath=COOPGISPath + 'mapserver/dynamic/'
COOPGISMapFileDBLines=\"\"\"CONNECTIONTYPE postgis\nCONNECTION 'host=localhost user=coopgisdbuser password=coopgis dbname=coopgis'\n\"\"\"
COOPGISGPSFileName="/opt/coopgis/fromgps/Locations.csv"
COOPGISSRID=4326
COOPGISGPSSRID=4326
COOPGISStakerWorkOrderReportPath='/var/www/coopgisstakerworkorders/'
COOPGISCISVendorCompatibleUnitFileName='/opt/coopgis/DataNotPartOfCOOPGIS/CISStaker.txt'
COOPGISBackupLocation='/opt/coopgisbackup/'
COOPGISFTPUpdateUserName='coopgisftpuser'
COOPGISFTPUpdatePassword='coopgis'
COOPGISDistanceMultiplier=1 #Multiplier used for all distance calculations, intended to be Conversion Factor to Convert Feet to Whatever unit used by the SRID
COOPGISTempImagePath='/opt/coopgis/media/tempimage/'
COOPGISStartupMusicSourceFileName='/opt/coopgis/media/music/coopgisstartup.mp3'
COOPGISStartupMusicDestinationFileName='/var/www/music/coopgisstartup.mp3'
COOPGISStartupMusicDestinationPathName='/var/www/music/'

DATABASES={}
DATABASES['default']={
#'ENGINE': 'django.db.backends.postgis',           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'coopgis',             # Or path to database file if using sqlite3.
'USER': 'coopgisdbuser',             # Not used with sqlite3.
'PASSWORD': 'coopgis',         # Not used with sqlite3.
'HOST': 'localhost',             # Set to empty string for localhost. Not used with sqlite3.
'PORT': '',             # Set to empty string for default. Not used with sqlite3.
}

class COOPGISDatabaseRouter(object):
    def db_for_read(self, model, **hints):
        try:
            using=model.using
        except:
            using='default'
        return using
    def db_for_write(self, model, **hints):
        try:
            using=model.using
        except:
            using='default'
        return using
    def allow_relation(self, obj1, obj2, **hints):
        try:
            using=model.using
        except:
            using='default'
        return using
    def allow_syncdb(self, db, model):
        try:
            using=model.using
        except:
            using='default'
        if(using == 'default' and db == 'default'):
            return True
        else:
            return False

DATABASE_ROUTERS=['coopgisconfig.COOPGISDatabaseRouter']"""

